/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"github.com/pkg/errors"
	"github.com/rs/zerolog/log"
	"github.com/spf13/viper"
	"kvmc/app"

	"github.com/spf13/cobra"
)

// startCmd represents the start command
var startCmd = &cobra.Command{
	Use:   "start",
	Short: "启动node",
	Long:  ``,
	Args: func(cmd *cobra.Command, args []string) error {
		if startOpt.Name == "" && len(args) > 0 {
			startOpt.Name = args[0]
		}
		if startOpt.Name == "" {
			return errors.New("node名称不能为空")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		manager, err := app.NewManager(viper.GetString("configPath"))
		if err != nil {
			log.Err(err).Send()
			return
		}
		manager.Start(startOpt)
	},
}

var startOpt = new(app.StartOption)

func init() {
	rootCmd.AddCommand(startCmd)

	startCmd.Flags().StringVarP(&startOpt.ClusterName, "cluster", "c", "", "node的集群名称")
	startCmd.Flags().StringVarP(&startOpt.Name, "name", "n", "", "node的名称")
	startCmd.Flags().BoolVarP(&startOpt.All, "all", "A", false, "启动集群中所有node")
}
